package com.anrongtec.zcpt.manager;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.adapter.EssenceRecycleAdapter;
import com.anrongtec.zcpt.base.BaseActivity;
import com.anrongtec.zcpt.base.DDApp;
import com.anrongtec.zcpt.module.AppModel;
import com.anrongtec.zcpt.util.Util;
import com.art.base.util.ApkUtils;
import com.art.base.util.DialogUtil;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

/**
 * 应用卸载
 * 
 * @author dongtianhao
 *
 */
public class UninstallActivity extends BaseActivity {
	private final static int STATUS_DATA_UPDATE = 1;
	private final static int STATUS_DATA_EMPTY = 2;
	private final static int STATUS_DATA_SEARCH = 3;
	private static final long DELAYED_TIME = 500;
	private EditText et_app_uninstall_search;
	private RecyclerView mRecyclerView;
	private EmptyWrapper empty;
	private List<AppModel> apps =new ArrayList<>();
	private List<AppModel> searchDate= new ArrayList<>();
	private String hint;
	private List<AppModel> subData;

	private EssenceRecycleAdapter mRecycleAdapter;

	private ProgressDialog mLoadingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_uninstall);
		initViews();
		initEvents();
		initData();
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void initViews() {
		et_app_uninstall_search = (EditText) findViewById(R.id.et_app_uninstall_search);
		mRecyclerView = (RecyclerView) findViewById(R.id.rv_app_uninstall_manager);

		LinearLayoutManager layoutManager = new LinearLayoutManager(this );
		//设置为垂直布局，这也是默认的
		layoutManager.setOrientation(OrientationHelper. VERTICAL);
		mRecyclerView.setLayoutManager(layoutManager);
		mRecycleAdapter = new EssenceRecycleAdapter(this,mOnAddPicClickListener);
		mRecyclerView.setAdapter(mRecycleAdapter);
	}

	private  EssenceRecycleAdapter.onAddPicClickListener mOnAddPicClickListener = new EssenceRecycleAdapter.onAddPicClickListener() {
		@Override
		public void onAddPicClick(int type, int position) {
			switch (type){
				case 0://卸载应用
					mLoadingDialog = DialogUtil.createLoadingDialog(UninstallActivity.this, "正在卸载...");
					ApkUtils.uninstall(UninstallActivity.this,apps.get(position).getAppPackName());
					mLoadingDialog.dismiss();
					apps.remove(position);
					break;
			}
		}
	};

	/***
	 * 初始化数据(已经安装的应用)
	 */
	@Override
	protected void initData() {
           apps = DDApp.getInstance().getAppModels();
           mRecycleAdapter.setData(apps);
	}

	@Override
	protected void initEvents() {

		et_app_uninstall_search.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(s.length()!=0){
					Util.post(searchThread);
				}else {
					mRecycleAdapter.setData(apps);
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}

	/**
	 * 搜索异步任务
	 */
	private Runnable searchThread = new Runnable() {
		@Override
		public void run() {
			String data = et_app_uninstall_search.getText().toString();
			final List<AppModel> dataSub = getDataSub(apps, data);
			//刷新ui
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					mRecycleAdapter.setData(dataSub);
				}
			});

	}
	};

	/**
	 * 搜索list里是否含有相应的APP对象
	 * 
	 * @param data
	 * @param key
	 */
	private List<AppModel> getDataSub(List<AppModel> data, String key) {
		if (subData == null) {
			subData = new ArrayList<AppModel>();
		} else {
			subData.clear();
		}
		int length = data.size();
		for (int i = 0; i < length; ++i) {
			if (data.get(i).getAppName().contains(key)) {
				subData.add(data.get(i));
			}
		}
		return subData;
	}

	@Override
	protected void onCreateCustomToolBar(Toolbar toolbar) {
		toolbar.setTitle(getString(R.string.title_activity_app_uninstall));
		super.onCreateCustomToolBar(toolbar);
	}

}
